<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="head_box">
        <img src="http://150.158.96.227/images/vue.jpg" alt="" />
      </div>
      <!-- 表单区域 -->
      <!-- ref 是表单的实例对象 -->
      <el-form
        :label-position="labelPosition"
        :model="loginForm"
        :rules="LoginRules"
        ref="loginFormRef"
      >
        <!-- prop 属性设置校验规则的字段名 加在item上而不是input-->
        <el-form-item prop="username" class="inputs first">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user-solid"
            autocomplete="off"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="password" class="inputs">
          <el-input
            v-model="loginForm.password"
            placeholder="请输入密码"
            show-password
            prefix-icon="el-icon-lock"
            autocomplete="off"
          >
          </el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button
            type="primary"
            class="login_btn"
            @click="submitForm()"
            :disabled="loginForm.dis"
            >登录</el-button
          >
          <el-button type="info" plain @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456",
        type: "",
        dis: false,
      },
      labelPosition: "right",

      LoginRules: {
        username: [
          // trigger 当鼠标失去焦点时校验此规则
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //  a 重置表单事件
    resetLoginForm() {
      // 这里的this指的是该组件的实例对象VueComponent 下面有一个$refs
     
      this.$refs.loginFormRef.resetFields();
    },
    // a 登录事件
    submitForm() {
      this.$refs.loginFormRef.validate(async (valid) => {
        // validate的形参 valid 返回的是布尔值
        if (valid) {
          //todo axios请求登录
          const res = await this.$http.post("login", {
            username: this.loginForm.username,
            password: this.loginForm.password,
          });
          if (res.data.meta.status != 200) {
            // * 错误消息弹窗
            this.$message.error("登录失败！");
          } else {
            // * 消息弹窗
            this.$message({
              message: "恭喜你，登录成功！",
              type: "success",
              duration: 1000,
              onClose: () => {
                this.$router.push("/Home");
              },
            });
            // *把token值存入sessionStroage中
            window.sessionStorage.setItem("token", res.data.data.token);
          }
        } else {
          this.$message.error("请输入正确的登录信息！");
          return false;
        }
      });
    },
  },
};
</script>

<style scoped lang="less">
* {
  margin: 0;
  padding: 0;
}
.login_container {
  width: 100%;
  height: 100%;
  background: url(http://150.158.96.227/images/bg.jpg) no-repeat;
  background-size: 100%;
  position: relative;
  .login_box {
    width: 450px;
    height: 300px;
    background-color: white;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -22%);
    .head_box {
      width: 100px;
      height: 100px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      box-sizing: border-box;
      box-shadow: 0 3px 10px 5px #eee;
      background-color: white;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      img {
        width: 100%;
        height: 100%;
        background-color: #eee;
        border-radius: 50%;
      }
    }
    .inputs {
      width: 70%;
      height: 30px;
      margin: 40px auto;
    }
    .first {
      margin-top: 70px;
    }
    .btns {
      display: flex;
      justify-content: flex-end;
      margin-right: 65px;
    }
    .login_btn {
      border: 1px solid rgb(176, 7, 8);
      background-color: rgb(176, 7, 8);
    }
  }
}
</style>
